﻿using MengLin.OOP.DesignPattern.Observer.TearcherPhoneChange.Implements;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MengLin.OOP.DesignPattern.Observer.TearcherPhoneChange
{
    class Program
    {
        static void Main(string[] args)
        {
            #region 传统写法-没有体现出老师变更了手机，主动通知学生
            //{
            //    Teacher zhangTeacher = new Teacher("123456789");//张老师
            //    //学生
            //    Student zhangSan = new Student("张三");
            //    Student liSi = new Student("李四");
            //    Student wangWu = new Student("王五");

            //    //通知学生(没有体现出主动告知学生老师的手机号码换了)
            //    zhangSan.TPhone = zhangTeacher.Phone;
            //    liSi.TPhone = zhangTeacher.Phone;
            //    wangWu.TPhone = zhangTeacher.Phone;

            //    zhangSan.Show();
            //    liSi.Show();
            //    wangWu.Show();

            //    //老师的手机号码变更
            //    zhangTeacher.Phone = "987654321";

            //    //通知学生
            //    zhangSan.TPhone = zhangTeacher.Phone;
            //    liSi.TPhone = zhangTeacher.Phone;
            //    wangWu.TPhone = zhangTeacher.Phone;

            //    zhangSan.Show();
            //    liSi.Show();
            //    wangWu.Show();
            //}
            #endregion

            #region 传统写法升级-不管我这个老师教不教我了，我里面还有老师这个类型
            //{
            //    //学生对象里面存了老师对象，也不是1个好的方案
            //    Teacher zhangTeacher = new Teacher("123456789");//张老师
            //    Student zhangSan = new Student("张三", zhangTeacher);
            //    Student liSi = new Student("李四", zhangTeacher);
            //    Student wangWu = new Student("王五", zhangTeacher);

            //    zhangSan.Show();
            //    liSi.Show();
            //    wangWu.Show();

            //    //老师的电话号码变更
            //    Console.WriteLine("============================");
            //    zhangTeacher.Phone = "987654321";

            //    zhangSan.Show();
            //    liSi.Show();
            //    wangWu.Show();
            //}
            #endregion

            #region 观察者模式写法-场景：老师电话变更通知所有学生
            {
                /***************场景：老师电话变更通知所有学生***************/
                Teacher zhangTeacher = new Teacher();//张老师
                Student zhangSan = new Student("张三");
                Student liSi = new Student("李四");
                Student wangWu = new Student("王五");
                JiaZhang jiaZhang = new JiaZhang("孟家长");

                //注册观察者：实际上是把学生，家长添加到老师的观察者集合里。
                zhangTeacher.RegisterObserver(zhangSan);
                zhangTeacher.RegisterObserver(liSi);
                zhangTeacher.RegisterObserver(wangWu);
                zhangTeacher.RegisterObserver(jiaZhang);//家长

                //老师的电话号码变更
                zhangTeacher.Phone = "123456789";
                //打印学生信息
                zhangSan.Show();
                liSi.Show();
                wangWu.Show();
                //打印家长信息
                jiaZhang.Show();

                //移除观察者：从观察者集合中移除张三同学
                zhangTeacher.UnRegisterObserver(zhangSan);
                Console.WriteLine("============================");
                //老师的电话号码再变更
                zhangTeacher.Phone = "987654321";

                //再打印学生信息
                zhangSan.Show();
                liSi.Show();
                wangWu.Show();
                //再打印家长信息
                jiaZhang.Show();
            }
            #endregion

            Console.ReadKey();
        }
    }
}
